package com.itheima.controller;

import com.itheima.common.PageResult;
import com.itheima.dto.QuestionsAuditsDto;
import com.itheima.dto.QuestionsDto;
import com.itheima.dto.QuestionsRecordsDto;
import com.itheima.pojo.*;
import com.itheima.service.QuestionsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("/questions")
public class QuestionsController {

    @Autowired
    private QuestionsService questionsService;

    @Autowired
    private HttpServletRequest request;

    /**
     * 审核意见
     * @param id
     * @return
     */
    @GetMapping("/{id}/auditOpinions")
    public QuestionsAuditsDto audiOpinions(@PathVariable Integer id) {
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");
        String username = user.getUsername();
        //todo 测试先把值写死
        QuestionsAuditsDto questionsAuditsDto = questionsService.audiOpinions(id,username);
        return questionsAuditsDto;
    }

    /**
     * 出题记录
     * @param id
     * @return
     */
    @GetMapping("/{id}/setRecords")
    public QuestionsRecordsDto setRecords(@PathVariable Integer id) {
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");
        String username = user.getUsername();
        //todo 测试先把值写死
        QuestionsRecordsDto questionsRecordsDto = questionsService.setRecords(id,username);
        return questionsRecordsDto;
    }

    /**
     * 基础题库的分页查询
     *
     * @param page
     * @param pageSize
     * @param questionsDto
     * @return
     */
    @GetMapping
    public PageResult findPage(Integer page, Integer pageSize, QuestionsDto questionsDto) {
        PageResult pageResult = questionsService.findPage(page, pageSize, questionsDto);
        return pageResult;
    }

    /**
     * 根据Id删除试题
     *
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public String deleteById(@PathVariable Integer id) {
        questionsService.deleteById(id);
        return "{success:true}";
    }

    /**
     * 新增基础试题
     *
     * @param questions
     * @return
     */
    @PostMapping
    public String insert(@RequestBody Questions questions) {
        questionsService.insert(questions);
        return "{id:" + questions.getId() + "}";
    }

    /**
     * 基础题库详情
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public QuestionsDto findById(@PathVariable Integer id) {
        QuestionsDto questionsDto = questionsService.findById(id);
        return questionsDto;
    }

    /**
     * 基础题库修改
     *
     * @param id
     * @param questionsDto
     * @return
     */
    @PutMapping("/{id}")
    public String updateById(@PathVariable Integer id, @RequestBody QuestionsDto questionsDto) {
        questionsService.updateById(id, questionsDto);
        return "{success:true}";
    }

    /**
     * 批量导入
     * @return
     */
    /*@PostMapping("/batch")
    public String addBatch() {

    }*/

    /**
     * 精选题库上下架
     *
     * @param id
     * @param publishState
     * @return
     */
    @PostMapping("/choice/{id}/{publishState}")
    public String modifyById(@PathVariable Integer id, @PathVariable Integer publishState) {
        questionsService.modifyById(id, publishState);
        return "{success:true}";
    }

    /**
     * 精选题库列表查询
     *
     * @param page
     * @param pageSize
     * @param questionsDto
     * @return
     */
    @GetMapping("/choice")
    public PageResult findPageChoice(Integer page, Integer pageSize, QuestionsDto questionsDto) {
        PageResult pageResult = questionsService.findPageChoice(page, pageSize, questionsDto);
        return pageResult;

    }

    /**
     * 组题列表查询
     *
     * @param page
     * @param pageSize
     * @param keyword
     * @return
     */
    @GetMapping("/randoms/")
    public PageResult findPageRandoms(Integer page, Integer pageSize, String keyword) {
        PageResult pageResult = questionsService.findPageRandoms(page, pageSize, keyword);
        return pageResult;
    }

    /**
     * 组题列表删除
     *
     * @param id
     * @return
     */
    @DeleteMapping("/randoms/{id}")
    public String deleteRandomsById(@PathVariable Integer id) {
        questionsService.deleteRandomsById(id);
        return "{success:true}";
    }

    /**
     * 加入或移出精选
     *
     * @param id
     * @param choiceState
     * @return
     */
    @PatchMapping("/choice/{id}/{choiceState}")
    public String patchById(@PathVariable Integer id, @PathVariable Integer choiceState) {
        questionsService.patchById(id, choiceState);
        return "{success:true}";
    }

    /**
     * 试题审核
     * @param id
     * @param questions
     * @return
     */
    @PostMapping("/check/{id}")
    public String updateCheckById(@PathVariable Integer id, @RequestBody Questions questions) {
        questionsService.updateCheckById(id, questions);
        return "{success:true}";
    }
}
